require(["common", "coAjax", "queryString", "Cookies", "href"], function(jSouper, coAjax, QueryString, Cookies, href) {
	App.set("$Cache.sign_in.name", Cookies.get("cache_user_login_name"));
	var queryString = new QueryString();
	// 切换登录者
	App.set("$Event.changloginType", function() {
		App.model.toggle("isBusLogin");
		if (App.get('isBusLogin')) {
			App.set("$Cache.logintype", 'bus');
		} else {
			App.set("$Cache.logintype", 'user');
		};
	});
	App.set('$Event.changlogin', function() {
		var logintype = App.get("$Cache.logintype");
		if (logintype == "bus") { //商家
			App.set("$Cache.sign_in.name", Cookies.get("cache_bus_login_name"));
		} else if (logintype == "user") { //买家
			App.set("$Cache.sign_in.name", Cookies.get("cache_user_login_name"));
		}
	});

	//默认是用户登录
	App.set("$Cache.logintype", "user");
	//切换到用户注册
	App.set("$Event.ChangeSign", function() {
		App.model.toggle('isUp');
	})
	if (queryString.get("id") == "2") {
		App.get("$Event.ChangeSign")();
	} else if (queryString.get("id") == "3") { //切换到商家登录
		App.set("$Cache.logintype", "bus");
		App.get('$Event.changlogin')();
	}
	/*
	 *登录
	 */

	App.set("$Event.sign_in.common_login", function() {
		var logintype = App.get("$Cache.logintype");
		debugger
		if (logintype == "bus") { //商家
			App.get("$Event.admin.login")();
		} else if (logintype == "user") { //买家
			App.get("$Event.sign_in.login")();
		}
	});

	//获取一个验证码
	function _loadCodeImg() {
		// $code.val("");
		// $signInImg.prop("src",appConfig.user.sign_in_img_code_url+"?_="+Math.random());
	};
	_loadCodeImg();

	App.set("$Event.sign_in.login", function() {
		var login_data = App.get("$Cache.sign_in");
		coAjax.post(appConfig.user.sign_in_url, login_data, function success(result) {
			alert("success", "登录成功");
			App.set("loginer", result.result);
			Cookies.set("cache_user_login_name", login_data.name);
			var callbackUrl = queryString.get("cb_url");
			if (callbackUrl) {
				href.jump(callbackUrl);
			} else {
				//到首页
				href.jump("/main-beta.html");
			}
			eventManager.fire("getLoginer");

		}, function error(errorCode, xhr, errorMsg) {
			alert("error", errorMsg);
			_loadCodeImg();
		});
	});

	// 卖家
	App.set("$Event.admin.login", function(argument) {
		var username = App.get("$Cache.sign_in.name");
		var password = App.get("$Cache.sign_in.password");
		coAjax.post(appConfig.bus.login, {
			name: username,
			password: password
		}, function() {
			alert("success", "商家登录成功");
			var callbackUrl = queryString.get("cb_url");
			Cookies.set("cache_bus_login_name", username);
			var callbackUrl = queryString.get("cb_url");
			//直接到商家后台
			href.jump("http://admin.dotnar.com");
		}, function(errorCode, xhr, errorMsg) {
			alert("error", errorMsg);
		})
	});
	App.set("$Event.sign_in.to_password_input", function() {
		App.set("$Cache.sign_in.is_focus", true);
		App.set("$Cache.sign_in.is_focus", false);
	});


	/*
	 * 注册
	 */
	App.set("$Event.sign_up.register", function() {
		if (!App.get('$Cache.sign_up.name')) {
			alert('warn', '请输入用户名')
			return;
		};
		if (!App.get('$Cache.sign_up.mobile_phone')) {
			alert('warn', '请输入手机号')
			return;
		};
		if (!App.get('$Cache.sign_up.email')) {
			alert('warn', '请输入邮箱')
			return;
		};
		if (!App.get('$Cache.sign_up.password')) {
			alert('warn', '请输入密码')
			return;
		};
		if (!App.get('$Cache.sign_up.confirm_password')) {
			alert('warn', '请再次输入密码')
			return;
		};
		if (!App.get('$Cache.sign_up.phone_code')) {
			alert('warn', '请输入验证码')
			return;
		};
		var register_data = App.get("$Cache.sign_up");
		coAjax.post(appConfig.user.sign_up_url, register_data, function(result) {
			alert("success", "注册成功");
			App.set("$Cache.sign_in.name", register_data.name);
			//清空表单
			App.set("$Cache.sign_up", {});
			//切换到登录
			App.get("$Event.sign_up.to_sign_in")();
		}, function(errorCode, xhr, errorMsg) {
			alert("error", errorMsg);
			//重新获取验证码
			App.set("$Cache.sign_up.wite_server", false);
		});

	});
	var _ti;

	function _phone_code_time(argument) {
		clearInterval(_ti);
		var _s = 60;
		_ti = setInterval(function() {
			_s -= 1;
			App.set("$Cache.sign_up.next_phone_code_time", _s);
			if (_s === 0) {
				clearInterval(_ti);
				App.set("$Cache.sign_up.wite_server", false);
			}
		}, 1000);
	}
	App.set("$Event.sign_up.get_register_code", function() {
		App.set("$Cache.sign_up.wite_server", true);
		coAjax.get(appConfig.user.register_code, {
			mobile_phone: App.get("$Cache.sign_up.mobile_phone")
		}, function(result) {
			alert(result.result);
			_phone_code_time();
		}, function(errorCode, xhr, errorMsg) {
			alert("error", errorMsg);
			App.set("$Cache.sign_up.wite_server", false);
		});
	});
	App.set('$Event.ApplyRule', function() {
		App.model.toggle('isApplyRules');
	});
	App.set("$Event.sign_up.checkedMobilePhone", function() {
		coAjax.get(appConfig.user.registerCheckedMobilePhone + App.get("$Cache.sign_up.mobile_phone"), function() {
			alert("success", "手机号码可用");
		});
	});
	App.set("$Event.sign_up.checkedEmail", function() {
		coAjax.get(appConfig.user.registerCheckedEmail + App.get("$Cache.sign_up.email"), function() {
			alert("success", "邮箱可用");
		});
	});
	App.set("$Event.findpaw", function() {
		href.jump("http://www.dotnar.com/find_paw.html?cb=" + location.host);
	})
});